/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2023 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "spi.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "u8g2/u8g2.h"
#include "button.h"
#include "f_spi.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
extern uint8_t u8x8_stm32_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
extern uint8_t u8x8_byte_stm32_hw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
extern int ENCODER_READ(void);
extern void value_change(int line,int ifadd);
extern void F_SPI_SendByte(uint8_t byte);
static u8g2_t u8g2;

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
//static uint8_t sw_k1 = 0;
//static uint8_t sw_k2 = 0;
//int sw_k1=0;
//int sw_k2=0;

//static uint8_t encoder = 0;
//static uint8_t encoder_s = 0;

int line=2;//表示改第几行的�??
int value=0;//表示�??????????
int wave_choose=0;
int hz=0;
int ampl=100;
int step=1;
int b=0;//编码器状�??????
uint8_t ifchange=0;
int right=0;
int left=0;
int encoder_poision_cnt=0;// 为脉冲计数器
int encoder_dir=0; //为方�?????????
int ampl_tmp=0;
static uint8_t wave_send=0;
static uint8_t hz1_send=0;//频率分成三段发送
static uint8_t hz2_send=0;
static uint8_t hz3_send=0;
static uint8_t ampl1_send=0;//幅值两段
static uint8_t ampl2_send=0;
static char step_char[10];
static char amp[8];
static char frequ[12];//频率输出文本
static char *wave[]={"sine","triangle","square"};
/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_SPI2_Init();
  MX_SPI1_Init();
  /* USER CODE BEGIN 2 */
  /* init u8g2 spi interface*/
  u8g2_Setup_ssd1306_128x64_noname_f(&u8g2, U8G2_R0, u8x8_byte_stm32_hw_spi, u8x8_stm32_gpio_and_delay);
  /* Initialize Display */
  u8g2_InitDisplay(&u8g2);
  u8g2_SetPowerSave(&u8g2, 0);
  u8g2_ClearBuffer(&u8g2);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
	ifchange=0;
	u8g2_ClearBuffer(&u8g2);
	u8g2_SetFont(&u8g2, u8g2_font_victoriamedium8_8r);

	//画个小圆圈表示�?�择
	u8g2_DrawCircle(&u8g2, 4, 16*line+7, 2, U8G2_DRAW_ALL);

	//波形
	u8g2_DrawStr(&u8g2, 10, 10, "wave");
	u8g2_DrawStr(&u8g2, 50, 10, wave[value]);

	//频率
	u8g2_DrawStr(&u8g2, 10, 26, "freq");
	itoa(hz,frequ,10);
	strcat(frequ,"Hz");
	u8g2_DrawStr(&u8g2, 50, 26, frequ);

	//频率调整步长
	u8g2_DrawStr(&u8g2, 10, 42, "step");
	itoa(step,step_char,10);
	u8g2_DrawStr(&u8g2, 50, 42, step_char);

	//编码�?????????
	if (b==1){
		value_change(line,0);
		b=0;
		ifchange=!ifchange;
	}
	else if (b==2){
		value_change(line,1);
		b=0;
		ifchange=!ifchange;
	}

	b=0;
	//幅�??
	u8g2_DrawStr(&u8g2, 10, 58, "ampl");
	itoa(ampl,amp,10);
	strcat(amp,"mV");
	u8g2_DrawStr(&u8g2, 50, 58, amp);
	u8g2_SendBuffer(&u8g2);

	//发送数据，给fpga

	wave_send=(uint8_t)(value >> 0);
	hz1_send=(uint8_t)(hz >> 0);
	hz2_send=(uint8_t)(hz >> 8);
	hz3_send=(uint8_t)(hz >> 16);

	ampl_tmp=ampl/100;
	ampl1_send=(uint8_t)(ampl_tmp >> 0);
	F_SPI_SendByte(wave_send);
	HAL_Delay(1);
	F_SPI_SendByte(hz1_send);
	HAL_Delay(1);
	F_SPI_SendByte(hz2_send);
	HAL_Delay(1);
	F_SPI_SendByte(hz3_send);
	HAL_Delay(1);
	F_SPI_SendByte(ampl1_send);

	HAL_Delay(200);
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV1;
  RCC_OscInitStruct.PLL.PLLN = 8;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
